package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.XcxUser;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.XcxUserMapper;
import com.ruoyi.system.service.IXcxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 小程序账户Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-29
 */
@Service
public class XcxUserServiceImpl implements IXcxUserService
{
    @Autowired
    private XcxUserMapper xcxUserMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public AjaxResult wxbindXcxUser(XcxUser xcxUser) {
        System.out.println(xcxUser.getWxId());
        XcxUser sizes=new XcxUser();
        sizes.setWxId(xcxUser.getWxId());
        List<XcxUser> list=xcxUserMapper.selectXcxUserList(sizes);
        if(list.size()!=0){
            return AjaxResult.warn("此微信已被绑定");
        }
        Calendar calendar= Calendar.getInstance();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        xcxUser.setCreateData(dateFormat.format(calendar.getTime()));
        return AjaxResult.success(xcxUserMapper.updateXcxUser(xcxUser));
    }

    /**
     * 查询小程序账户
     *
     * @param xcxUserId 小程序账户主键
     * @return 小程序账户
     */
    @Override
    public XcxUser selectXcxUserByXcxUserId(Long xcxUserId)
    {
        return xcxUserMapper.selectXcxUserByXcxUserId(xcxUserId);
    }

    /**
     * 查询小程序账户列表
     *
     * @param xcxUser 小程序账户
     * @return 小程序账户
     */
    @Override
    public List<XcxUser> selectXcxUserList(XcxUser xcxUser)
    {
        return xcxUserMapper.selectXcxUserList(xcxUser);
    }

    /**
     * 新增小程序账户
     *
     * @param xcxUser 小程序账户
     * @return 结果
     */
    @Override
    public int insertXcxUser(XcxUser xcxUser)
    {
        return xcxUserMapper.insertXcxUser(xcxUser);
    }

    /**
     * 修改小程序账户
     *
     * @param xcxUser 小程序账户
     * @return 结果
     */
    @Override
    public int updateXcxUser(XcxUser xcxUser)
    {
        return xcxUserMapper.updateXcxUser(xcxUser);
    }

    /**
     * 批量删除小程序账户
     *
     * @param xcxUserIds 需要删除的小程序账户主键
     * @return 结果
     */
    @Override
    public int deleteXcxUserByXcxUserIds(Long[] xcxUserIds)
    {
        return xcxUserMapper.deleteXcxUserByXcxUserIds(xcxUserIds);
    }

    /**
     * 删除小程序账户信息
     *
     * @param xcxUserId 小程序账户主键
     * @return 结果
     */
    @Override
    public int deleteXcxUserByXcxUserId(Long xcxUserId)
    {
        return xcxUserMapper.deleteXcxUserByXcxUserId(xcxUserId);
    }

    /**
     * 删除小程序账户信息
     *
     * @param xcxUserId 小程序账户主键
     * @return 结果
     */
    public int deleteXcxUserByXcxUserIds(Long xcxUserId)
    {
        SysUser sysUser=new SysUser();
        int randomNum = (int) ((Math.random() * 9 + 1) * 100000);
        sysUser.setPassword(SecurityUtils.encryptPassword(randomNum+""));
        return sysUserMapper.updateUser(sysUser);
    }
}